﻿using CoreWebApi.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace CoreWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Route("home/[controller]")]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {

        [HttpGet]
        public string Get(string name)
        {
            var config = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                   .Build();
            string type = config.GetSection("DefaultAnimal").Value;
            Type tp = Type.GetType(type);
            object obj = Activator.CreateInstance(tp, name);
            return $"{obj.ToString()}{Environment.NewLine}at {Request.Headers["dt"]}!";
        }

        [HttpPost("h")]
        public string Hello(string name)
        {
            var config = new ConfigurationBuilder()
                 .SetBasePath(Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .Build();
            string type = config.GetSection("DefaultAnimal").Value;
            Type tp = Type.GetType(type);
            object obj = Activator.CreateInstance(tp, name);
            return $"{obj.ToString()}{Environment.NewLine}at {Request.Headers["dt"]}!";
        }

        [HttpPost("h2")]
        [HttpGet("h3")]
        public string Hello2(string name)
        {
            return $"Hello,{name}!{Environment.NewLine}at {Request.Headers["dt"]}!";
        }


        [HttpGet("h4")]
        ResponseInfo Hello4(string name)
        {
            return new ResponseInfo()
            {
                //data = new ResponseInfo()
                //,
                success = true
                ,
                message = $"Hello,{name}!{Environment.NewLine}at {Request.Headers["dt"]}!"
            };
        }


    }
}
